#!/bin/bash

# User set variables.
# Change these to match your system config.
RRDTOOL="/usr/bin/rrdtool"
IPVSADM="/sbin/ipvsadm"
WORKDIR="/home/lvs-rrd/performance"

# You should not have to change anything below. 
###############################################
cd $WORKDIR
IPs=""

while read VAL1 VAL2 VAL3 VAL4 VAL5 VAL6 VAL7; do
	if [ "$VAL1" = "TCP" ] || [ "$VAL1" = "UDP" ] ; then
		SERVICE="`echo $VAL2|sed s/:/./`"
              	PROTOCOL="$VAL1"
		VIP="`echo $VAL2|sed s/://`"
	elif [ "$VAL3" = "Route"  ] || [ "$VAL3" = "Masq"  ] || \
	     [ "$VAL3" = "Tunnel" ] || [ "$VAL3" = "Local" ]; then
		REAL="`echo $VAL2|sed s/:/./`"
		NAME="`echo $VIP$VAL2|sed s/://`"
		IPs="$IPs $SERVICE.$REAL.$PROTOCOL"
		eval A$NAME="$VAL5"
		eval I$NAME="$VAL6"
	fi
done < /proc/net/ip_vs 

for i in $IPs;do
	if [ ! -f lvs.$i.rrd ]; then
		echo -n "RRD file missing... "
		if [ -f lvs.`echo $i|sed "s/\.TCP$//g"|sed "s/\.UDP$//g"`.rrd ]; then
			echo "No worries. Just an old filename. Updating..."
			cp -a lvs.`echo $i|sed "s/\.TCP$//g"|sed "s/\.UDP$//g"`.rrd lvs.$i.rrd
		else
			echo "Creating RRD file lvs.$i.rrd"
			$RRDTOOL create lvs.$i.rrd -s 60 \
				DS:Active:GAUGE:180:0:U \
				DS:Inactive:GAUGE:180:0:U \
				RRA:AVERAGE:0.5:1:600 \
				RRA:AVERAGE:0.5:6:700 \
				RRA:AVERAGE:0.5:24:775 \
				RRA:AVERAGE:0.5:144:775 \
				RRA:AVERAGE:0.5:288:797 \
				RRA:AVERAGE:0.5:576:797 \
				RRA:AVERAGE:0.5:1152:800 \
				RRA:MAX:0.5:1:600 \
				RRA:MAX:0.5:6:700 \
				RRA:MAX:0.5:24:775 \
				RRA:MAX:0.5:144:775 \
				RRA:MAX:0.5:288:797 \
				RRA:MAX:0.5:576:797 \
				RRA:MAX:0.5:1152:800 \
				RRA:MIN:0.5:1:600 \
				RRA:MIN:0.5:6:700 \
				RRA:MIN:0.5:24:775 \
				RRA:MIN:0.5:144:775 \
				RRA:MIN:0.5:288:797 \
				RRA:MIN:0.5:576:797 \
				RRA:MIN:0.5:1152:800
		fi
	fi

        TEMP=`echo $i|sed "s/TCP$//g"|sed "s/UDP$//g"|sed "s/\.//g"`
        VARS[0]=A$TEMP
        VARS[1]=I$TEMP
        for j in 0 1;do
                if [ -z ${!VARS[$j]} ]; then
                        eval $VARS[$j]=U
                fi
        done
	echo $RRDTOOL update $WORKDIR/lvs.$i.rrd N:${!VARS[0]}:${!VARS[1]}
        $RRDTOOL update $WORKDIR/lvs.$i.rrd N:${!VARS[0]}:${!VARS[1]}
done
